package com.example.wangyi.mvp.ui.fragment.home;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.ColumnLayoutHelper;
import com.bumptech.glide.Glide;
import com.example.wangyi.R;
import com.example.wangyi.mvp.model.bean.HomeBean;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HomeBannerAdapter extends DelegateAdapter.Adapter {

    private ColumnLayoutHelper columnLayoutHelper;
    private ArrayList<HomeBean.DataDTO.BannerDTO> banner;

    public HomeBannerAdapter(ColumnLayoutHelper columnLayoutHelper, ArrayList<HomeBean.DataDTO.BannerDTO> banner) {

        this.columnLayoutHelper = columnLayoutHelper;
        this.banner = banner;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return columnLayoutHelper;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new HomeBannerViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_home_banner, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof HomeBannerViewHolder){
            for (int i = 0; i < banner.size(); i++) {
                Log.e("TAG", "onBindViewHolder: "+banner.get(i).getImage_url());
            }

            ((HomeBannerViewHolder) holder).homeBanner.setImages(banner)
                    .setImageLoader(new ImageLoader() {
                        @Override
                        public void displayImage(Context context, Object path, ImageView imageView) {
                            HomeBean.DataDTO.BannerDTO bannerDTO = (HomeBean.DataDTO.BannerDTO) path;
                            Glide.with(context).load(bannerDTO.getImage_url())
                                    .into(imageView);
                        }
                    }).start();
        }
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    static
    class HomeBannerViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.home_banner)
        Banner homeBanner;

        HomeBannerViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
